from django.db import models
from django.contrib.auth.models import User
from DjangoUeditor.models import UEditorField

# Create your models here.
class Article(models.Model):
    title = models.CharField(max_length=100,verbose_name='文章标题')
    excerpt = models.TextField('摘要', max_length=200, blank=True)
    category = models.ForeignKey(verbose_name='文章分类表',to='Category',on_delete=models.DO_NOTHING,blank=True,null=True)
    tags = models.ManyToManyField(verbose_name='文章标签表',to='Tag',blank=True)
    img = models.ImageField(upload_to='article_img/%Y/%m/%d/', verbose_name='文章图片', blank=True, null=True)
    body = UEditorField('文章内容', width=800, height=500,
                    toolbars="full", imagePath="upimg/", filePath="upfile/",
                    upload_settings={"imageMaxSize": 1204000},
                    settings={}, command=None, blank=True
                    )
    user = models.ForeignKey(User,verbose_name='作者',on_delete=models.CASCADE)
    views = models.PositiveIntegerField(verbose_name="文章浏览数",default=0)
    tui = models.ForeignKey(verbose_name='推荐位',to='Tui',on_delete=models.DO_NOTHING,blank=True,null=True)
    created_time = models.DateTimeField(verbose_name='文章发布时间',auto_now_add=True)
    modified_time = models.DateTimeField('修改时间', auto_now=True)

    class Meta:
        verbose_name = '文章表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Category(models.Model):
    name = models.CharField(verbose_name='博客分类',max_length=30)
    index = models.IntegerField(default=999, verbose_name='分类排序')

    class Meta:
        verbose_name='博客分类表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Tag(models.Model):
    name = models.CharField(max_length=30,verbose_name='标签')

    class Meta:
        verbose_name = '文章标签表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Tui(models.Model):
    name = models.CharField(max_length=100,verbose_name='推荐位')

    class Meta:
        verbose_name = '推荐位表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Banner(models.Model):
    text_info = models.CharField(max_length=100,verbose_name='图片文本信息',default='')
    img = models.ImageField(upload_to='banner/',verbose_name='图片路径')
    link_url = models.URLField(verbose_name='图片链接的URL',max_length=100)
    is_active = models.BooleanField(verbose_name='激活',default=False)

    class Meta:
        verbose_name = '轮播图'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.text_info


class Link(models.Model):
    name = models.CharField(max_length=20,verbose_name='友情链接的名称')
    link_url = models.URLField(verbose_name='友情链接的URL',max_length=100)

    class Meta:
        verbose_name = '友情链接'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Comment(models.Model):
    content = models.TextField(verbose_name='评论内容')
    articleid = models.ForeignKey(to='Article',verbose_name='被评论的文章',on_delete=models.DO_NOTHING)
    is_active =models.BooleanField(verbose_name='激活',default=True)
    pid = models.ForeignKey('self',verbose_name='父评论',on_delete=models.DO_NOTHING,null=True,blank=True)
    created_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = '评论表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content
